//
//  AroundViewController.m
//  DialectShare
//
//  Created by timmy zhong on 3/20/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AroundViewController.h"
#import "ASIHTTPRequest.h"
#import "DialectDB.h"
#import "DialectCell.h"
#import "DialectProxy.h"
#import "ListenViewController.h"

@interface AroundViewController ()
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) CLLocation *location;
@property (nonatomic, retain) UILabel *locationLabel;
- (void)refresh;
@end

@implementation AroundViewController
@synthesize locationManager = _locationManager, location = _location, locationLabel = _locationLabel;

- (void)dealloc
{
    self.location = nil;
    self.locationManager = nil;
    self.locationLabel = nil;
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"刷新" style:UIBarButtonItemStylePlain target:self action:@selector(refresh)] autorelease];
        
    float height = 24;
    CGRect frame = CGRectMake(0, self.view.frame.size.height-height, self.view.frame.size.width, height);
    UIView *view = [[UIView alloc] initWithFrame:frame];
    [self.view addSubview:view];
    [view setBackgroundColor:[UIColor grayColor]];
    view.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [view release];
    frame = self.tableView.frame;
    frame.size.height -= height;
    self.tableView.frame = frame;

    float imageWidth = 16;
    frame = CGRectMake(10, (height - imageWidth) / 2.0, imageWidth, imageWidth);
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.image = [UIImage imageNamed:@"green_point.png"];
    [view addSubview:imageView];
    [imageView release];
    
    frame = CGRectMake(CGRectGetMaxX(imageView.frame)+10, 0, view.frame.size.width - 2*imageView.frame.origin.x - imageView.frame.size.width, height);
    _locationLabel = [[UILabel alloc] initWithFrame:frame];
    _locationLabel.textColor = [UIColor whiteColor];
    _locationLabel.font = [UIFont systemFontOfSize:15];
    _locationLabel.backgroundColor = [UIColor clearColor];
    _locationLabel.text = @"test";
    [view addSubview:_locationLabel];
    
    if ([CLLocationManager locationServicesEnabled]) {
        
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.distanceFilter = 200;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [_locationManager startUpdatingLocation];
        
    } else {
        
        NSLog(@"Fail to start location service");
        
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.distanceFilter = 200;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [_locationManager startUpdatingLocation];
    }
    
    [self.proxy aroundLongitude:120 latitude:23 page:1 count:1000];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSArray *)getDialects
{
    return [self.db aroundWithLongitude:120.0f latitude:23.0f start:0 count:1000];
}

- (void)refresh
{
    [self.proxy aroundLongitude:120 latitude:23 page:1 count:1000];
}

#pragma mark - location delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    self.location = newLocation;
    NSLog(@"%@", self.location);    
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"fail to update location  %@", error.localizedDescription);
}







@end
